<?php

namespace App\V3\Api\Models;

use App\V3\Api\Models\SellDriverModel;
use DateTimeInterface;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class SellModel extends Model
{
    use HasFactory;

    protected $table = 'sell';

    protected $dateFormat = 'U';//这个时间字段被为被格式化为UNIX 时间戳的形式存储
    //toArray 时间转换问题
    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }
    /**
     * $dates 数组内的列转换为日期格式
     *
     * @var array
     */
    protected $dates = [
        'sell_date',
        'deliver_time',
        'order_time',
        'collection_time'
    ];

    public function materielInfo()
    {
        return SELF::hasMany(SellGoodsDetailModel::class, 'sell_id', 'id');
    }

    public function packageInfo()
    {
        return SELF::hasMany(SellPackageDetailModel::class, 'sell_id', 'id');
    }

    public function driverInfo()
    {
        return SELF::hasOne(SellDriverModel::class, 'sell_id', 'id');
    }

    static public function getSellInfo($userId)
    {
        return SELF::select('id')->where($userId)->from('sell as a')->get()->toArray();
    }

    static public function getSellIdInfo($userId, $data, $condition)
    {
        return SELF::select('id')
            ->from('sell as a')
            ->where($condition)
            ->where($userId)->get()->toArray();
    }

    static public function detail($id)
    {
        return SELF::select(
            'a.*',
            'b.username',
            'b.nickname'
        )
            ->from('sell as a')
            ->leftJoin('admin as b', 'b.id', '=', 'a.user_id')
            ->with('materielInfo', function($query) {
                $query->select(
                    'sell_goods_detail.id',
                    'sell_goods_detail.id as sell_goods_id',
                    'sell_goods_detail.materiel_type_id',
                    'a.materiel_name as name',
                    'a.type_name',
                    'a.materiel_no',
                    'a.materiel_img as image',
                    'price',
                    'sell_goods_detail.num',
                    'sell_goods_detail.out_num',
                    'b.storage_name as warehouse',
                    'b.id as warehouse_id',
                    'piece',
                    'sell_id',
                    'cost_price',
                    'sell_goods_detail.materiel_type_id',
                    'sell_goods_detail.shelf_id',
                    'sell_goods_detail.location_id',
                    's.shelf_name',
                    'l.location_name',
                    'a.is_use_serial'
                )
                    ->with('goodsBatchInfo', function($query) {
                        $query->select(
                            'sell_goods_detail_batch.id',
                            'storage_batch_id',
                            'batch_no',
                            'a.storage_manage_id',
                            'sell_goods_detail_id',
                            'sell_id',
                            'a.main_id',
                            'sell_goods_detail_batch.num',
                            'rest_num'
                        )
                        ->leftJoin('storage_batch as a', 'a.id', '=', 'sell_goods_detail_batch.storage_batch_id');
                    })
                    ->leftJoin('materiel_type as a', 'a.id', '=', 'sell_goods_detail.materiel_type_id')
                    ->leftJoin('storage as b', 'b.id', '=', 'sell_goods_detail.warehouse_id')
                    ->leftJoin('storage_shelf as s','sell_goods_detail.shelf_id','=','s.id')
                    ->leftJoin('storage_shelf_location as l','sell_goods_detail.location_id','=','l.id');
            })
            ->with('packageInfo', function($query) {
                $query->select(
                    'sell_package_detail.id',
                    'sell_package_detail.id as sell_package_id',
                    'package_id',
                    'package_name',
                    'price',
                    'total_cost',
                    'sell_id',
                    'num',
                    'out_num',
                    'package_no',
                    'piece'
                )
                    ->leftJoin('package as p', 'p.id', '=', 'sell_package_detail.package_id')
                    ->with('packageContent', function($query) {
                        $query->select(
                            'sell_package_content.id',
                            'sell_package_content.id as sell_package_content_id',
                            'sell_package_content.materiel_class_id',
                            'sell_package_content.storage_id',
                            'sell_package_content.package_content_id',
                            'sell_package_content.storage_manage_id',
                            'sell_package_content.materiel_type_id',
                            'package_detail_id',
                            'mt.materiel_name',
                            'mt.type_name',
                            'mt.selling_price',
                            'mt.materiel_no',
                            'mt.materiel_img',
                            'mt.cost',
                            'mcs.class_name as s_class_name',
                            'mcs.class_no as s_class_no',
                            'mcf.class_name as f_class_name',
                            'mcf.class_no as f_class_no',
                            's.storage_name',
                            'sm.in_storage_num',
                            'sell_package_content.num',
                            'sell_package_content.total_num',
                            'sell_package_content.shelf_id',
                            'sell_package_content.location_id',
                            'se.shelf_name',
                            'lo.location_name',
                            'mt.is_use_serial'
                        )
                            ->with('packageBatchInfo', function($query) {
                                $query->select(
                                    'sell_package_batch.id',
                                    'sell_package_batch.sell_package_content_id',
                                    'sell_package_batch.storage_manage_id',
                                    'sell_package_batch.storage_batch_id',
                                    'sell_package_batch.sell_id',
                                    'sell_package_batch.main_id',
                                    'sell_package_batch.num',
                                    'sell_package_batch.rest_num',
                                    'batch_no',
                                )
                                    ->leftJoin('storage_batch as a', 'a.id', '=', 'sell_package_batch.storage_batch_id');
                            })
                            ->leftJoin('materiel_type as mt', 'mt.id', '=', 'sell_package_content.materiel_type_id')
                            ->leftJoin('materiel_class as mcs', 'mcs.id', '=', 'sell_package_content.materiel_class_id')
                            ->leftJoin('materiel_class as mcf', 'mcf.id', '=', 'mcs.parent_id')
                            ->leftJoin('storage_manage as sm', 'sm.id', '=', 'sell_package_content.storage_manage_id')
                            ->leftJoin('storage as s', 's.id', '=', 'sell_package_content.storage_id')
                            ->leftJoin('storage_shelf as se','sell_package_content.shelf_id','=','se.id')
                            ->leftJoin('storage_shelf_location as lo','sell_package_content.location_id','=','lo.id');
                    });
            })
            ->with('driverInfo', function($query) {
                $query->select(
                    'id',
                    'plate_number',
                    'name',
                    'phone',
                    'logistics_company',
                    'remark',
                    'plate_image',
                    'id_card_image',
                    'driving_licence',
                    'sell_id',
                );
            })
            ->where('a.id', $id)->first();
    }
}
